//
//  EnterView.m
//  登录界面
//
//  Created by Cloud on 2017/3/25.
//  Copyright © 2017年 Cloud. All rights reserved.
//

#import "EnterView.h"
#import "Masonry.h"
#import "HMbounceTool.h"
#import "HMLeftViewController.h"
#import "HMRightViewController.h"
#import "EnterMsg.h"
#import "HMTool.h"
#import "HMLoginModel.h"
#import "HMDefaultsTool.h"

@interface EnterView ()<UITextFieldDelegate>

@property(weak, nonatomic)UITextField *phone;
@property(weak, nonatomic)UITextField *password;

@property (nonatomic, strong)NSString *phoneNum;
@property (nonatomic, strong)NSString *passNum;

@end

@implementation EnterView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:77/255.0 green:194/255.0 blue:156/255.0 alpha:1];
    
    _phone.delegate = self;
    _password.delegate = self;
    
    [self setupUI];
}

- (void)setupUI
{
 
#pragma mark - 账号密码UI
    UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo"]];
    [self.view addSubview:imgV];
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.height.mas_offset(80);
        make.top.equalTo(self.view).mas_offset(100);
    }];
    
    UIImageView *phoneV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mobile"]];
    [self.view addSubview:phoneV];
    [phoneV sizeToFit];
    [phoneV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).mas_offset(32);
        make.top.equalTo(self.view).mas_offset(225);
    }];
    
    UIView *v1 = [[UIView alloc] init];
    v1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:v1];
    [v1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_offset(310);
        make.height.mas_offset(1);
        make.top.equalTo(phoneV.mas_bottom).mas_offset(6);
    }];
    
    UITextField *text1 = [[UITextField alloc] init];
    text1.placeholder = @"请输入您的手机号码";
    [text1 setFont:[UIFont systemFontOfSize:14]];
    _phone = text1;
    [self.view addSubview:text1];
    [text1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(phoneV);
        make.left.equalTo(phoneV.mas_right).mas_offset(10);
    }];
    
    UIImageView *passwordV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"password"]];
    [self.view addSubview:passwordV];
    [passwordV sizeToFit];
    [passwordV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(v1).mas_offset(24);
        make.left.equalTo(self.view).mas_offset(32);
    }];
    
    UIView *v2 = [[UIView alloc] init];
    v2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:v2];
    [v2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_offset(310);
        make.height.mas_offset(1);
        make.top.equalTo(passwordV.mas_bottom).mas_offset(6);
    }];
    
    UITextField *text2 = [[UITextField alloc] init];
    text2.placeholder = @"请输入您的登录密码";
    [text2 setFont:[UIFont systemFontOfSize:14]];
    _password = text2;
    text2.secureTextEntry = YES;
    [self.view addSubview:text2];
    [text2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(passwordV);
        make.left.equalTo(passwordV.mas_right).mas_offset(10);
    }];
    
#pragma mark - 登录UI
//    UIButton *msgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [msgBtn setTitle:@"通过短信验证码登录" forState:UIControlStateNormal];
//    [msgBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [msgBtn setFont:[UIFont systemFontOfSize:12]];
//    [self.view addSubview:msgBtn];
//    [msgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.view);
//        make.top.equalTo(v2.mas_bottom).mas_offset(12);
//    }];
//    
    UIButton *enterBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    enterBtn.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.25];
    [enterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [enterBtn setTitle:@"登录" forState:UIControlStateNormal];
    [enterBtn setFont:[UIFont systemFontOfSize:14]];
    [self.view addSubview:enterBtn];
    [enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(270);
        make.height.mas_offset(42);
        make.centerX.equalTo(self.view);
        make.top.equalTo(v2.mas_bottom).mas_offset(58);
    }];
    [enterBtn addTarget:self action:@selector(enter:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *escBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    escBtn.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.25];
    [escBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [escBtn setTitle:@"取消" forState:UIControlStateNormal];
    [escBtn setFont:[UIFont systemFontOfSize:14]];
    [self.view addSubview:escBtn];
    [escBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(270);
        make.height.mas_offset(42);
        make.centerX.equalTo(self.view);
        make.top.equalTo(enterBtn.mas_bottom).mas_offset(12);
    }];
    [escBtn addTarget:self action:@selector(down:) forControlEvents:UIControlEventTouchUpInside];
    
#pragma mark - 注册与忘记密码
    UIView *v3 = [[UIView alloc] init];
    v3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:v3];
    [v3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(1);
        make.height.mas_offset(13);
        make.centerX.equalTo(self.view);
        make.top.equalTo(enterBtn.mas_bottom).mas_offset(120);
    }];
    
    UIButton *newBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [newBtn setTintColor:[UIColor whiteColor]];
    [newBtn setTitle:@"注册新账号" forState:UIControlStateNormal];
    [newBtn setFont:[UIFont systemFontOfSize:13]];
    [self.view addSubview:newBtn];
    [newBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(v3);
        make.centerX.equalTo(self.view).mas_offset(-self.view.bounds.size.width * 0.25);
        
    }];
    
    UIButton *passwordBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [passwordBtn setTintColor:[UIColor whiteColor]];
    [passwordBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [passwordBtn setFont:[UIFont systemFontOfSize:13]];
    [self.view addSubview:passwordBtn];
    [passwordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(v3);
        make.centerX.equalTo(self.view).mas_offset(self.view.bounds.size.width * 0.25);
    }];
}



- (void)down:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)enter:(UIButton *)sender
{
    if ([_phone.text isEqualToString:@""]) {
    
        [HMbounceTool CKLShowSVErrorTitle:@"账号不能为空" andTime:1.5];
    }else if([_password.text isEqualToString:@""]){
        [HMbounceTool CKLShowSVErrorTitle:@"密码不能为空" andTime:1.5];
    }else
    {
        [HMbounceTool CKLShowSVSuccessTitle:@"登陆成功" andTime:1.5];
        NSString *phoneNum = _phone.text;
        _phoneNum = phoneNum;
        NSString *passNum = _password.text;
        _passNum = passNum;
        [self dismissViewControllerAnimated:YES completion:nil];
        [self.delegate enterView:self];
        
        [self enterYes];
        
        EnterMsg *msg = [EnterMsg new];
        msg.enter = @"yes";
        
        NSString *entPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"enter.h"];
        [NSKeyedArchiver archiveRootObject:msg toFile:entPath];
        NSLog(@"===============%@",entPath);
        NSLog(@"===============%@",msg.enter);
    }
}

- (void)enterYes
{
    [[HMTool sharedNetworkTool] POSTWithURLString:@"http://iosapi.itcast.cn/doctor/carelinkQuickLogin.json.php" parameters:@{@"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f"} success:^(id responseObject) {
        
        //反序列化
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:NULL];
        
        //获取成功
        if ([result[@"msg"] isEqualToString:@"OK"]) {
            HMLoginModel *loginModel = [HMLoginModel loginWithDict:result[@"data"]];
            NSLog(@"===========%@",loginModel);
            
            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:loginModel];
            
            [HMDefaultsTool saveObject:data forKey:[NSString stringWithFormat:@"%@-MODEL",_phoneNum]];
            [HMDefaultsTool saveObject:_phoneNum forKey:_phoneNum];
            [HMDefaultsTool saveObject:_phoneNum forKey:@"currentID"];
        
        }
    } faile:^(NSError *error) {
        NSLog(@"网络请求失败：%@",error);
    }];
}
#pragma mark - 设置回收键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [_password resignFirstResponder];
    [_phone resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    return YES;
}

@end
